﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2023_09_13
{
    internal class TreasureChests
    {
        static int flag = 0;
        static void Main3(string[] args)
        {
            Console.Write("请输入数组的数量: ");
            int n = Convert.ToInt32(Console.ReadLine());
            List<int[]> arrays = GetArrays(n);
            FindMix(arrays, int.MaxValue, 0);
            int sum = 0;
            for (int i = 0; i < arrays.Count; i++)
            {
                if (i == flag)
                    sum += arrays[i][0];
                else
                    sum += arrays[i][1];
            }
            Console.WriteLine(sum);
        }

        private static void FindMix(List<int[]> arrays, int minnum, int bit)
        {
            if (bit >= arrays.Count)
                return;
            if (arrays[bit][1] - arrays[bit][0] < minnum)
            {
                minnum = arrays[bit][1] - arrays[bit][0];
                flag = bit;
            }
            FindMix(arrays, minnum, bit + 1);
        }

        static List<int[]> GetArrays(int n)
        {
            List<int[]> arrays = new List<int[]>();
            for (int i = 0; i < n; i++)
            {
                Console.Write("请输入数组 " + (i + 1) + " 的两个数字，用空格隔开: ");
                string[] input = Console.ReadLine().Split(' ');
                if (Convert.ToInt32(input[0]) > Convert.ToInt32(input[1]))
                {
                    string tmp = input[0];
                    input[0] = input[1];
                    input[1] = tmp;
                }
                int[] array = { Convert.ToInt32(input[0]), Convert.ToInt32(input[1]) };
                arrays.Add(array);
            }
            return arrays;
        }
    }
}
